package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.*;
import com.zy.imagineCommunity.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class PostController {

    @Autowired
    PostService postService;

    @GetMapping("/post")
    private String getPost() {
        return "/post";
    }

    @ResponseBody
    @GetMapping("/addPost")
    public void addPost(String title, String content, String type, int userId) {

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //增加帖子
        postService.addPost(new Post(null, title, content, type, userId, simpleDateFormat.format(date)));
    }

    @ResponseBody
    @GetMapping("/postList")
    public JSONObject postList(int pageNo) {
        System.out.println("进来了啊！");
        //查询这一页的信息
        Page<PostText> page = postService.queryPostByPageNoAndPageSize(pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 到帖子详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/postDetails")
    private String postDetails(int id, Model model) {

        model.addAttribute("id", id);

        return "/postDetails";
    }

    /**
     * 帖子详情
     *
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getPostDetails")
    private JSONObject getPostDetails(int id) {
        JSONObject jsonObject = new JSONObject();

        PostText postText = postService.queryPostById(id);

        jsonObject.put("postText", postText);

        return jsonObject;
    }

    /**
     * 聊天列表
     *
     * @param postId
     * @return
     */
    @ResponseBody
    @GetMapping("/getChatList")
    private JSONObject getChatList(int postId, int pageNo) {
        JSONObject jsonObject = new JSONObject();

        Page<ChatText> page = postService.queryChatTextByPostIdAndBeginAndPageSize(postId, pageNo, 10);

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 增加聊天
     *
     * @param postId
     * @return
     */
    @ResponseBody
    @GetMapping("/addChat")
    private void addChat(int postId, String content, int userId) {

        System.out.println(postId + content + userId);

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        postService.addChat(new Chat(null, postId, content, simpleDateFormat.format(date), userId));

    }

    @GetMapping("/personalPost")
    private String getPersonalPost() {
        return "/personalPost";
    }

    @ResponseBody
    @GetMapping("/personalPostList")
    public JSONObject personalPostList(int userId, int pageNo) {
        System.out.println("进来了啊！");
        //查询这一页的信息
        Page<PostText> page = postService.queryPostByUserIdAndPageNoAndPageSize(userId, pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 删除帖子
     *
     * @param postId
     */
    @ResponseBody
    @GetMapping("/deletePersonalPost")
    public void deletePersonalPost(int postId) {
        //删除帖子
        postService.deletePostById(postId);
        //删除帖子下面的聊天信息
        postService.deleteChatByPostId(postId);
    }

    /**
     * 删除帖子
     *
     * @param chatId
     */
    @ResponseBody
    @GetMapping("/deleteChat")
    public void deleteChat(int chatId) {
        //删除帖子下面的聊天信息
        postService.deleteChatByChatId(chatId);
    }

    /**
     * 搜索帖子
     *
     * @param pageNo
     * @param searchValue
     * @return
     */
    @ResponseBody
    @GetMapping("/searchPostList")
    public JSONObject searchPostList(int pageNo, String searchValue) {

        //查询这一页的信息
        Page<PostText> page = postService.queryPostByPageNoAndPageSizeAndSearchValue(pageNo, 10, searchValue);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 搜索个人帖子
     *
     * @param userId
     * @param pageNo
     * @return
     */
    @ResponseBody
    @GetMapping("/searchPersonalPost")
    public JSONObject searchPersonalPost(int userId, int pageNo, String searchValue) {
        System.out.println("进来了啊！");
        //查询这一页的信息
        Page<PostText> page = postService.searchPostByUserIdAndPageNoAndPageSizeAndSearchValue(userId, pageNo, 10, searchValue);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

}
